package cn.script.game.dup;

import cn.script.call_api.AfterApi;
import cn.script.call_api.BeforeApi;
import cn.script.common.CoordConst;
import cn.script.domain.Point;
import cn.script.domain.RangePoint;
import cn.script.exception.StopException;
import cn.script.game.GameOpt;
import cn.script.game.opt.DefaultOpt;
import cn.script.game.prof.Pi;
import cn.script.util.Delay;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 天堂副本
 *
 * @author 周永波
 */
public class TtDup extends AbstractDup {

    private GameOpt gop;

    private String teamName;

    private String cityName;

    private ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue());

    public TtDup(Pi pi, GameOpt go, GameOpt gop, String cityName, String teamName) {
        super("神秘天堂", pi, go);
        this.gop = gop;
        this.cityName = cityName;
        this.teamName = teamName;
    }

    @Override
    protected BeforeApi beFore() {
//        return null;
        return () -> {
            // 404 185 410 262
            // 当在这个范围点击 出现对话框时证明点击成功
//            this.gop.toCity(this.cityName);
            this.go.clickNpcTt(RangePoint.rp(400, 173, 446, 260));
            this.go.text_click("神秘的天堂");
            this.go.text_click("开启通往天堂之门");
            return 1;
        };
    }

    @Override
    protected AfterApi after() {

        return () -> {
            this.go.toBack();
            Delay.delay(5000);
            return 1;
        };
    }

    protected void termBack(){
        poolExecutor.execute(()->{
            this.gop.toCity(cityName);
        });
    }

    protected void clickNpc(RangePoint rp){
        int num = 0;
        while (true){
            if (num!=0 && num%25==0){
                DefaultOpt.rgSkill(this.go,rp,1);
                Delay.delay(500);
            }
            if (num > 130){
                throw new StopException("NPC 点击不到 退出副本");
            }
            this.go.wio().moveToEx(rp,100);
            this.go.wio().rightClick(100);
            String attackName = this.go.gi().attackName();
            if (attackName.contains("诺亚方舟")){
                this.go.wio().leftClick(100);
                Delay.delay(300);
                this.go.text_click("下一");
                return;
            }
            num++;
        }


    }


    @Override
    public DupInfo dupInfo() {
        DupInfo dupInfo = new DupInfo(this.go, this.optName, this.pi.skills());
        dupInfo.dupOpt("日本方舟(首领)")
                .a_endNum(0)
                .a_timeOut(20000)
                .a_range(130, 339, 215, 346)
                .beFore(() -> {
                    termBack();
                    return 1;
                })
                .after(() -> {
                    this.go.wio().leftClick(Point.point(404, 288), 1000);
                    this.go.text_click("下一关");
                    return 1;
                });

        dupInfo.dupOpt("美国方舟(首领)")
                .m_winPoint(566, 369)
                .m_gamePoint(166, 85)
                .m_rPoint(4, 4)
                .a_endNum(4)
                .a_timeOut(30000)
                .a_range(276,173,592,244)
                .beFore(() -> {
                    termBack();
                    DefaultOpt.defaultSkill(this.go, 1);
                    return 1;
                }).after(() -> {
            this.go.moveTo(this.optName)
                    .winPoint(425, 380)
                    .gamePoint(136, 125)
                    .rPoint(2, 2).opt();
            clickNpc(RangePoint.rp(486,251,530,306));
            return 1;
        });


        dupInfo.dupOpt("伊拉克方舟(首领)")
                .m_winPoint(153, 300)
                .m_gamePoint(28, 153)
                .m_rPoint(2, 2)
                .a_endNum(4)
                .a_timeOut(15000)
                .beFore(() -> {
                    termBack();
//                    DefaultOpt.rgSkill(this.go, RangePoint.rp(617, 184, 714, 285), 1);
                    this.go.moveTo(this.optName)
                            .winPoint(189, 319)
                            .gamePoint(47, 154)
                            .rPoint(2, 2)
                            .opt();
                    DefaultOpt.rgSkill(this.go, RangePoint.rp(0, 0, 150, 50), 2);
                    Delay.delay(500);
                    DefaultOpt.defaultSkill(this.go, 1);
                    Delay.delay(500);
                    DefaultOpt.rgSkill(this.go, RangePoint.rp(0, 0, 150, 50), 2);
                    Delay.delay(1000);
                    return 1;
                }).after(() -> {
            this.go.moveTo(this.optName)
                    .winPoint(261, 310)
                    .gamePoint(60, 131)
                    .rPoint(2, 2).opt();
            clickNpc(RangePoint.rp(388,288,421,333));
//            Delay.delay(1000);
//            this.go.npc("诺亚方舟");
//            this.go.text_click("勇闯下一区域");
            return 1;
        });
//
        dupInfo.dupOpt("意大利方舟(首领)")
                .m_winPoint(354, 253)
                .m_gamePoint(55, 79)
                .m_rPoint(2, 2)
                .a_endNum(2)
                .a_timeOut(20000)
                .a_range(393,309,491,370)
                .moveAfter(() -> {
                    DefaultOpt.rgSkill(this.go, RangePoint.rp(561, 550, 570,550), 3);
                    return 1;
                })
                .beFore(() -> {
                    termBack();
//                    DefaultOpt.defaultSkill(this.go, 1);

//                    DefaultOpt.rgSkill(this.go, RangePoint.rp(398, 213, 410, 213), 4);
                    return 1;
                }).after(() -> {
            this.go.moveTo(this.optName)
                    .winPoint(318, 238)
                    .gamePoint(39, 81)
                    .rPoint(2, 2).opt();
            clickNpc(RangePoint.rp(490,296,566,387));
            Delay.delay(3000);
//            this.go.npc("诺亚方舟");
//            this.go.text_click("勇闯下一区域");
            return 1;
        });
        dupInfo.dupOpt("比利时方舟(首领)")
                .m_winPoint(454, 281)
                .m_gamePoint(93, 67)
                .m_rPoint(2, 2)
                .a_endNum(5)
                .a_timeOut(15000)
                .a_range(689, 385, 689, 385)
                .beFore(() -> {
                    termBack();
                    DefaultOpt.defaultSkill(this.go, 1);
                    DefaultOpt.rgSkill(this.go, RangePoint.rp(800, 300, 800, 300), 2);
                    return 1;
                }).after(() -> {
//            this.go.moveTo(this.optName)
//                    .winPoint(451, 287)
//                    .gamePoint(96, 72)
//                    .rPoint(2, 2).opt();

//            this.go.npc("亚方舟");
            this.go.wio().leftClick(Point.point(400, 293), 1000);
            int ret = this.go.text_click("勇闯下一区域");
            return 1;
        });

        dupInfo.dupOpt("俄罗斯方舟(首领)")
                .m_winPoint(565, 307)
                .m_gamePoint(135, 54)
                .m_rPoint(4, 4)
                .a_endNum(3)
                .a_timeOut(12000)
                .a_range(578, 144, 771, 254)
                .moveAfter(() -> {
                    DefaultOpt.defaultSkill(this.go, 2);
                    return 1;
                })
                .beFore(() -> {
                    termBack();
                    DefaultOpt.defaultSkill(this.go, 1);
                    this.go.moveTo(this.optName)
                            .winPoint(550, 325)
                            .gamePoint(139, 66)
                            .rPoint(4, 4)
                            .after(() -> {
                                DefaultOpt.defaultSkill(this.go, 8);
                                return 1;
                            }).opt();
                    return 1;
                })
                .after(() -> {
                    Delay.delay(2000);
                    return 1;
                });

        return dupInfo;
    }


}
